<template>
  <div id="trash" class="detail">
    <div class="note-sidebar">
      <div class="header">
        <h3>回收站</h3>
      </div>
      <div class="menu">
        <span>更新时间</span>
        <span>标题</span>
      </div>
      <ul class="notes">
        <li v-for="note in trashNotes" :key="note.id">
          <router-link :to="`/trash?noteId=${note.id}`">
            <span>{{ note.updatedAtFriendly }}</span>
            <span>{{ note.title ? note.title : '' }}</span>
          </router-link>
        </li>
      </ul>
    </div>
    <div class="note-detail">
      <div style="height:100%">
        <div class="note-bar">
          <span>
            <span>创建日期：{{ curTrashNote.createdAtFriendly }}</span>
            <span> | </span>
            <span>更新日期：{{ curTrashNote.updatedAtFriendly }}</span>
            <span> | </span>
            <span>所属笔记本：{{ belongTo }}</span>
          </span>
          <span>
            <a @click="onRevert">恢复</a>
            <a @click="onDelete">彻底删除</a>
          </span>
        </div>
        <div class="note-main">
          <div class="note-title">
            <span>{{ curTrashNote.title }}</span>
          </div>
          <div class="editor">
            <div class="preview markdown-body" v-html="compiledMarkdown"></div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import MarkdownIt from 'markdown-it'
import { mapGetters, mapMutations, mapActions } from 'vuex'
let md = new MarkdownIt()
export default {
  data() {
    return {}
  },
  created() {
    this.checkLogin({ path: '/login' }),
      this.getNotebooks(),
      this.getTrashNotes().then(() => {
        this.setCurTrashNote({ curTrashNoteId: this.$route.query.noteId })
        this.$router.replace({
          path: '/trash',
          query: {
            noteId: this.curTrashNote.id,
          },
        })
      })
  },
  methods: {
    ...mapMutations(['setCurTrashNote']),
    ...mapActions([
      'checkLogin',
      'deleteTrashNote',
      'revertTrashNote',
      'getTrashNotes',
      'getNotebooks',
    ]),
    onRevert() {
      this.revertTrashNote({ noteId: this.curTrashNote.id }).then(() => {
        this.setCurTrashNote()
        this.$router.replace({
          path: '/trash',
          query: {
            noteId: this.curTrashNote.id,
          },
        })
      })
    },
    onDelete() {
      this.$confirm('确定删除笔记？', '此操作无法恢复', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.deleteTrashNote({ noteId: this.curTrashNote.id }).then(() => {
            this.setCurTrashNote()
            this.$router.replace({
              path: '/trash',
              query: {
                noteId: this.curTrashNote.id,
              },
            })
          })
        })
        .catch((err) => {
          console.log(err)
        })
    },
  },
  // 局部路由守卫
  beforeRouteUpdate(to, from, next) {
    this.setCurTrashNote({ curTrashNoteId: to.query.noteId })
    next()
  },
  computed: {
    ...mapGetters(['trashNotes', 'curTrashNote', 'belongTo']),
    compiledMarkdown() {
      console.log(this.curTrashNote.content, 6666)
      return md.render(this.curTrashNote.content || '')
    },
  },
}
</script>

<style lang="scss" scoped>
.detail {
  display: flex;
  height: 100%;
  width: 100vw;
  .note-detail {
    width: 100%;
    background: #fff;
  }
  .note-bar {
    padding: 4px 20px;
    border-bottom: 1px solid #eee;
    display: flex;
    justify-content: space-between;

    span,
    a {
      font-size: 12px;
      color: #999;
      margin-right: 4px;
      margin-left: 4px;
    }
    a {
      cursor: pointer;
    }
  }
  .note-main {
    height: calc(100% - 66px);
    padding: 10px 20px;
    width: 100%;
    font-size: 14px;
    .note-title {
      font-size: 18px;
      margin-bottom: 20px;
    }
    .markdown-body {
      overflow: hidden;
    }
  }
}
.note-sidebar {
  min-width: 290px;

  border-right: 1px solid #ccc;
  background-color: #eee;
  height: 100%;

  .header {
    height: 45px;
    display: flex;
    justify-content: center;
    align-items: center;
    border-bottom: 1px solid #ccc;
    background-color: #f7f7f7;
    width: 100%;
    padding: 0 6px;
    h3 {
      font-weight: normal;
    }
  }
  .menu {
    font-size: 12px;
    border-bottom: 1px solid #ccc;
    display: flex;
    span {
      padding: 2px 10px;
      width: 100%;
    }
    :first-child {
      border-right: 1px solid #ccc;
    }
  }
  .notes {
    margin-top: 6px;

    li {
      width: 100%;
      a {
        width: 100%;
        display: flex;
        font-size: 12px;
        span {
          width: 100%;
          padding: 4px 10px;
        }
      }
      .router-link-exact-active {
        border: 2px solid #81c0f3;
        border-radius: 3px;
        width: 100%;
      }
    }
    li:nth-child(2n-1) {
      background-color: #f2f2f2;
    }
  }
}
</style>
